using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene.Voice
{
	public class UploadVoiceContext : IContextBase, IRecorderContext
	{
		private const string TAG = "UploadVoiceContext";
		public const int STATUS_INIT = 0;
		public const int STATUS_READY = 1;
		public const int STATUS_SENDING = 2;
		public const int STATUS_ERROR = 4;
		public const int STATUS_COMPLETE = 5;
		private const int MAX_SEND_BYTE_PER_PACK = 6000;
		private const int MIN_SEND_BYTE_PER_PACK = 2000;
		private const int MIN_SAVE_BLOCK = 20000;
		public const int FINISHED_NULL = 0;
		public const int FINISHED_SUCCESS = 1;
		public const int FINISHED_ERROR = 2;
		public const int FINISHED_SHORTVOICE = 3;
		private UploadVoiceProgress mVoiceProgress = new UploadVoiceProgress();
		public NetSceneUploadVoice mScene;
		private MsgTrans _mMsgTrans;
		private byte[] mOutputBuffer = new byte[65536];
		private object mOutputBufLock = new object();
		public int mSendingLength;
		public int mRemainAmrBlock;
		public ChatMsg mChatMsg;
		public bool mIsCancelled;
		private int mFinishedType;
		public string mUserName
		{
			get
			{
				return this._mMsgTrans.strToUserName;
			}
			set
			{
				this._mMsgTrans.strToUserName = value;
			}
		}
		public string mClientMsgId
		{
			get
			{
				return this._mMsgTrans.strClientMsgId;
			}
		}
		public int mMsgSrvId
		{
			get
			{
				return this._mMsgTrans.nMsgSvrID;
			}
			set
			{
				this._mMsgTrans.nMsgSvrID = value;
			}
		}
		public int mCreateTime
		{
			get
			{
				return (int)this._mMsgTrans.nCreateTime;
			}
			set
			{
				this._mMsgTrans.nCreateTime = (long)((ulong)value);
			}
		}
		public int mVoiceTimeLength
		{
			get
			{
				return this._mMsgTrans.nDuration;
			}
			set
			{
				if (value > this._mMsgTrans.nDuration)
				{
					this._mMsgTrans.nDuration = value;
				}
			}
		}
		public int mTotalLength
		{
			get
			{
				return this._mMsgTrans.nTotalDataLen;
			}
			set
			{
				this._mMsgTrans.nTotalDataLen = value;
			}
		}
		public int mNetOffset
		{
			get
			{
				return this._mMsgTrans.nTransDataLen;
			}
			set
			{
				this._mMsgTrans.nTransDataLen = value;
			}
		}
		public int mStatus
		{
			get
			{
				return this._mMsgTrans.nStatus;
			}
			set
			{
				this._mMsgTrans.nStatus = value;
			}
		}
		public int mEndFlag
		{
			get
			{
				return this._mMsgTrans.nEndFlag;
			}
			set
			{
				this._mMsgTrans.nEndFlag = value;
				this.updateToVoiceTransMsg();
				this.updateToVoiceChatMsg();
			}
		}
		public int mOutputLength
		{
			get
			{
				return this._mMsgTrans.nRecordLength;
			}
			set
			{
				this._mMsgTrans.nRecordLength = value;
			}
		}
		private UploadVoiceContext()
		{
		}
		public static UploadVoiceContext createByMsgTrans(MsgTrans msgTrans)
		{
			return new UploadVoiceContext
			{
				_mMsgTrans = msgTrans
			};
		}
		public static UploadVoiceContext createByClientMsgID(string clientMsgID)
		{
			MsgTrans byClientMsgID = StorageMgr.msgVoice.getByClientMsgID(clientMsgID);
			if (byClientMsgID == null)
			{
				return UploadVoiceContext.createByMsgTrans(new MsgTrans
				{
					nTransType = 3,
					strClientMsgId = clientMsgID
				});
			}
			return UploadVoiceContext.createByMsgTrans(byClientMsgID);
		}
		public void printfInfo()
		{
		}
		public bool isRecordEnd()
		{
			return this.mEndFlag == 1;
		}
		public bool isEncodeAmrEnd()
		{
			return this.mEndFlag == 1 && this.mRemainAmrBlock == 0;
		}
		public bool needToHandle()
		{
			return this.mStatus == 0;
		}
		public bool needToClean()
		{
			return this.mFinishedType != 0;
		}
		public bool isSendCompleted()
		{
			return UploadVoiceContext.isSendCompleted(this._mMsgTrans);
		}
		public bool isSaveCompleted()
		{
			return this.mRemainAmrBlock == 0 && UploadVoiceContext.isSaveCompleted(this._mMsgTrans);
		}
		public static bool needResumeFromTrans(MsgTrans msgTrans, uint uint_0)
		{
			if (msgTrans.nStatus == 5)
			{
				Log.smethod_5("UploadVoiceContext", "comleted task, ignored.");
				return false;
			}
			if (!UploadVoiceContext.isSaveCompleted(msgTrans))
			{
				Log.smethod_5("UploadVoiceContext", "not saved task, ignored.");
				return false;
			}
			return true;
		}
		public static bool needCleanFromTrans(MsgTrans msgTrans, uint uint_0)
		{
			return !UploadVoiceContext.needResumeFromTrans(msgTrans, uint_0);
		}
		public static bool isSendCompleted(MsgTrans msgTrans)
		{
			return msgTrans.nTransDataLen == msgTrans.nRecordLength && msgTrans.nEndFlag == 1;
		}
		public static bool isSaveCompleted(MsgTrans msgTrans)
		{
			return msgTrans.nTotalDataLen == msgTrans.nRecordLength && msgTrans.nEndFlag == 1;
		}
		public static void printInfo(MsgTrans msgTrans)
		{
			Log.smethod_5("UploadVoiceContext", string.Concat(new object[]
			{
				"task info: savedlen =",
				msgTrans.nTotalDataLen,
				", recordedlen=",
				msgTrans.nRecordLength,
				", sentlen = ",
				msgTrans.nTransDataLen,
				", voicelen = ",
				msgTrans.nDuration,
				", endflag = ",
				msgTrans.nEndFlag
			}));
		}
		public bool isRunning()
		{
			return this.mStatus != 0 && this.mFinishedType == 0;
		}
		public bool isFinished()
		{
			return this.mFinishedType != 0;
		}
		public bool isInvalidShortVoice()
		{
			return this.mVoiceTimeLength < 1000;
		}
		public bool appendOutputData(byte[] buffer, int offset, int count)
		{
			bool result;
			lock (this.mOutputBufLock)
			{
				if (count <= 0)
				{
					result = true;
				}
				else
				{
					if (this.mOutputLength + count > this.mOutputBuffer.Length)
					{
						int num = this.mOutputBuffer.Length;
						byte[] array = this.mOutputBuffer;
						if (num >= 131072)
						{
							Log.smethod_1("UploadVoiceContext", "recorder voice buffer overflow,stop recorded. ");
							result = false;
							return result;
						}
						this.mOutputBuffer = new byte[num * 2];
						Buffer.BlockCopy(array, 0, this.mOutputBuffer, 0, num);
					}
					Buffer.BlockCopy(buffer, offset, this.mOutputBuffer, this.mOutputLength, count);
					this.mOutputLength += count;
					result = true;
				}
			}
			return result;
		}
		public byte[] getBufferFromHead()
		{
			byte[] result;
			lock (this.mOutputBufLock)
			{
				int num = 6000;
				int num2 = this.mOutputLength - this.mNetOffset;
				if (6000 > num2)
				{
					num = num2;
				}
				if (num < 2000 && !this.isEncodeAmrEnd())
				{
					result = null;
				}
				else
				{
					if (num <= 0)
					{
						result = null;
					}
					else
					{
						byte[] array = new byte[num];
						Buffer.BlockCopy(this.mOutputBuffer, this.mNetOffset, array, 0, num);
						this.mSendingLength = num;
						result = array;
					}
				}
			}
			return result;
		}
		private void updateToVoiceTransMsg()
		{
			if (!this.isInvalidShortVoice() && this._mMsgTrans != null && !this.mIsCancelled)
			{
				StorageMgr.msgVoice.updateTransByClientMsgId(this._mMsgTrans);
				return;
			}
		}
		public void onFinished(int finishType)
		{
			this.mFinishedType = finishType;
			this.updateToVoiceTransMsg();
			this.updateToVoiceChatMsg();
		}
		public void checkRecorderSaver()
		{
			int num = this.mOutputLength - this.mTotalLength;
			if (this.isInvalidShortVoice())
			{
				return;
			}
			this.checkAddVoiceChatMsg();
			if (num < 20000 && !this.isEncodeAmrEnd())
			{
				return;
			}
			byte[] array = new byte[num];
			Buffer.BlockCopy(this.mOutputBuffer, this.mTotalLength, array, 0, num);
			StorageMgr.msgVoice.saveVoiceBlockData(this._mMsgTrans, array, this.mChatMsg);
			Log.smethod_4("UploadVoiceContext", string.Concat(new object[]
			{
				"saved to file, offset = ",
				this.mTotalLength,
				", size = ",
				num
			}));
			this.mTotalLength += num;
		}
		public bool checkAddVoiceChatMsg()
		{
			if (this.mChatMsg == null && !this.mIsCancelled)
			{
				this.mChatMsg = new ChatMsg();
				this.mChatMsg.strMsg = "";
				this.mChatMsg.strTalker = this._mMsgTrans.strToUserName;
				this.mChatMsg.nMsgType = 34;
				this.mChatMsg.nStatus = this.convertStatus(this.mStatus);
				this.mChatMsg.nCreateTime = (long)this.mCreateTime;
				this.mChatMsg.nIsSender = 1;
				this.mChatMsg.strClientMsgId = this.mClientMsgId;
				this.updateChatMsgContent();
				if (StorageMgr.chatMsg.addMsg(this.mChatMsg))
				{
					this._mMsgTrans.nMsgLocalID = this.mChatMsg.nMsgLocalID;
					this.updateToVoiceTransMsg();
				}
				else
				{
					this.mChatMsg = null;
					Log.smethod_1("UploadVoiceContext", "add chat-msg failed.");
				}
				return true;
			}
			return false;
		}
		public void updateToVoiceChatMsg()
		{
			if (this.mChatMsg != null && !this.mIsCancelled)
			{
				Log.smethod_5("UploadVoiceContext", "update  voice-chat msg....timelength = " + this.mVoiceTimeLength);
				this.mChatMsg.nStatus = this.convertStatus(this.mStatus);
				this.mChatMsg.nMsgSvrID = this.mMsgSrvId;
				this.updateChatMsgContent();
				StorageMgr.chatMsg.modifyMsg(this.mChatMsg);
				return;
			}
		}
		private int convertStatus(int status)
		{
			MsgUIStatus result = MsgUIStatus.Processing;
			if (status == 5)
			{
				result = MsgUIStatus.Success;
			}
			else
			{
				if (status == 4)
				{
					result = MsgUIStatus.Fail;
				}
			}
			return (int)result;
		}
		private void updateChatMsgContent()
		{
			this.mChatMsg.strMsg = string.Concat(new object[]
			{
				"<msg><voicemsg  endflag=\"",
				this.mEndFlag,
				"\" length=\"",
				this.mTotalLength,
				"\" voicelength=\"",
				this.mVoiceTimeLength,
				"\" clientmsgid=\"",
				this.mClientMsgId,
				"\" fromusername=\"",
				this.mUserName,
				"\" downcount=\"",
				this.mNetOffset,
				"\" /></msg>"
			});
		}
		public bool loadFromMsgTrans()
		{
			byte[] array = StorageMgr.msgVoice.loadVoiceFileData(this._mMsgTrans);
			if (array == null)
			{
				Log.smethod_1("UploadVoiceContext", "load from msgtrasn failed , no data found ");
				this.mStatus = 5;
				StorageMgr.msgVoice.updateTransByClientMsgId(this._mMsgTrans);
				return false;
			}
			this.mChatMsg = StorageMgr.chatMsg.getMsg(this._mMsgTrans.nMsgLocalID);
			if (this.mChatMsg == null)
			{
				Log.smethod_1("UploadVoiceContext", "failed to load chat msg by clientmsgid , ignored task. ");
				this.mStatus = 5;
				StorageMgr.msgVoice.updateTransByClientMsgId(this._mMsgTrans);
				return false;
			}
			Log.smethod_5("UploadVoiceContext", "load upvoice from msgtrans success. ");
			this.mOutputBuffer = array;
			this.mSendingLength = 0;
			this.mStatus = 0;
			this.updateToVoiceChatMsg();
			return true;
		}
		public void onStartup(int creatTime, object args)
		{
			this.mCreateTime = creatTime;
		}
		public void onVoiceTimeChanged(int timeLength)
		{
			this.mVoiceTimeLength = timeLength;
		}
		public void onStop(byte[] voiceBuffer, int dataLength, int timeLength)
		{
			this.mEndFlag = 1;
			if (this.mScene != null)
			{
				this.mScene.dispatchVoiceHandler();
			}
			if (this.mIsCancelled)
			{
				Log.smethod_5("UploadVoiceContext", "on stop with cancelled..");
				if (this.mChatMsg != null)
				{
					StorageMgr.chatMsg.delMsg(this.mChatMsg);
				}
				if (this._mMsgTrans != null)
				{
					StorageMgr.msgVoice.method_2(this._mMsgTrans);
				}
			}
		}
		public bool isNeedData()
		{
			return !this.isFinished();
		}
		public bool onAppendOutputData(byte[] buffer, int offset, int count)
		{
			return this.appendOutputData(buffer, offset, count);
		}
	}
}
